from flask import request
from flask_classy import FlaskView, route

from ..schema.resp import language_and_currency_schema
from ..services.currency import CurrencyService
from ..services.language import LanguageService


class SettingsView(FlaskView):
    @route('language_and_currency')
    def get_language_and_currency(self):
        language_arg = request.args.get('language', None)
        currency_arg = request.args.get('currency', None)
        ip_arg = request.args.get('ip', None)

        language = None
        currency = None
        if language_arg:
            language = LanguageService.get_by_id(language_arg)

            if not language:
                language_arg = language_arg.replace('-', '_')
            language = LanguageService.get_by_id(language_arg)

        if not language:
            language = LanguageService.get_default()

        if currency_arg:
            currency = CurrencyService.get_currency_by_id(currency_arg)

        # TODO 需要给 language  指定默认的币种？

        # if not currency and language.currency:
        #     currency = language.currency

        if not currency:
            currency = CurrencyService.get_main_currency()

        return language_and_currency_schema.jsonify({
            'language': language,
            'currency': currency
        })
